package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.model.User;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

//@Controller //我是一个控制器,我要在spring启动的时候加载并注册
//@Component
//@ResponseBody// 当前返回的是非静态页面
@RestController
//@RequestMapping("/web") //当使用"/web"可以访问到当前页=-
public class WebController {
    //@RequestMapping(value = "/hi",method = RequestMethod.POST)//当使用"/web"+"/hi"时可以访问到当前方法
    //@PostMapping("/hi")
    @GetMapping("/hi")
    public Object sayHi(HttpServletRequest request, HttpServletResponse response){
        return "Hi,"+request.getParameter("name");
    }

    /**
     *获取单个参数
     * @param name
     * @return
     */
    @GetMapping("/get1")
    public String getParam1(String name){
        return "value:"+name;

    }


    /**
     * 获取多个参数
     * @param name
     * @param age
     * @return
     */
    @GetMapping("/get2")
    public String getParam2(String name,Integer age){
        //执行到这里时就已经得到了前端传递的参数的值了,就可以随意使用了
        return "name: "+name+" age: "+age;//返回的顺序无所谓了
    }

    /**
     * 获取的参数类型为对9象
     * @param student
     * @return
     */
    @GetMapping("/get3")
    public String getParam(Student student){
        return student.toString();
    }

    /**
     * 获取form表单中的参数
     * @param name
     * @param password
     * @return
     */
    @RequestMapping("/login")//该注解全支持(get,post)
    public String login(String name,String password){
        return "<h1>用户名:"+name+" | 密码:"+password+"</h1>";
    }

    /**
     * 获取form表单参数(易混).不是json对象
     * @param name
     * @param password
     * @return
     */
    @RequestMapping("/login2")
    public HashMap<String,Object> login2(String name,String password){
        HashMap<String,Object> result=new HashMap<>();
        result.put("name",name);
        result.put("password",password);
        return result;
    }


    /*
     * 得到用户传递的json对象
     * @param user
     * @return
     */
    @RequestMapping("/login3")
    public HashMap<String,Object> login3(@RequestBody User user){
        HashMap<String,Object> result=new HashMap<>();
        result.put("name",user.getName());
        result.put("password",user.getPassword());
        return result;
    }


    @RequestMapping("/register")
    public User register(User user){
        return user;
    }

    /**
     * 获取文件
     */
    @RequestMapping("/reg2")
    public String reg2(String name, @RequestPart("myfile")MultipartFile file) throws IOException {//myfile是上传的文件名,与前端一致
        //保存图片
        file.transferTo(new File("D:\\work\\picture.jpg"));//保存的路径
        return "success";
    }

    /**
     * spring mvc 获取cookie值
     *
     */

    @RequestMapping("/getcookie")
    public String getCookie(@CookieValue("zl110") String zl
    ,@CookieValue("java") String java){//拿到前端的 zl110 赋值给zl
        return "zl:"+zl+"| java:"+java;
    }

    /**
     * 设置session(新旧一样)
     * @param request
     * @return
     */
    @RequestMapping("/setsession")
    @ResponseBody
    public String setsession(HttpServletRequest request){
        //获取HttpSession对象,参数设置为true,表示如果没有session对象就会创建一个session
        HttpSession session=request.getSession(true);
        if(session!=null){
            session.setAttribute("username","java");
        }
        return "session 存储成功";
    }

    /**
     * 读取session(新方式)
     */
    @RequestMapping("/getsession")
    public String getsession(@SessionAttribute(value = "username",required = false) String username){
        return "username:"+username;
    }


    @RequestMapping("/gethead")
    public String getHead(@RequestHeader("User-Agent") String userAgent){
        return "User-Agent:"+userAgent;

    }


    @RequestMapping("/gettime")
    public String getTime(@RequestParam(value = "t",required = false) String time){
        return "time:"+time;
    }


    @RequestMapping("/login4/{name}/{password}")
    public String login4(@PathVariable("name") String username,@PathVariable("password")String password ){
        return "naxmeeebbbbbe:"+username+" | password:"+password;
    }





}
